var express = require('express');
var conn = require('../public/javascripts/connect');
var judge_login = require('../public/javascripts/judge_login');
var router = express.Router();
router.get("/", async function (req, res) {
    let Jurisdiction = null;
    let user = {};
    if (req.cookies.jinshan_rabbing) {
        Jurisdiction = await judge_login.judgelogin(req.cookies.jinshan_rabbing);
        console.log(Jurisdiction);
        if (Jurisdiction) {
            user.department_number = Jurisdiction.department_number;
            user.real_name = Jurisdiction.real_name;
        }
    }
    ;
    // console.log("usersfsdbfs", req.query)
    let {tableName} = req.query,
        tableName1 = `${tableName.split('_')[0]}_${Number(tableName.split('_')[1]) - 1}_${tableName.split('_')[2]}`
    let sql2 = `SELECT * FROM check_date WHERE table_name='${tableName}' or table_name='${tableName1}'`;
    let resultData = await new Promise((resP) => {
        conn.connection2.query(sql2, function (err, result, fields) {
            if (err) {
                console.log('[SELECT ERROR] - ', err.message);
                return;
            }
            resP(result);
        })
    });
    for (let index in resultData) {
        for (let item in resultData[index]) {
            for (let item1 in resultData[index][item]) {
                resultData[index]['table_name'] = resultData[index]['table_name'].split('_')[0];
                resultData[index]['picture_path'] = resultData[index]['picture_path'].toString().split(',');
                resultData[index]['Detailed'] = resultData[index]['Detailed'].toString().split(',');
                if (resultData[index]['grabbed_data'] == resultData[index]['validation_data']) {
                    resultData[index]['result'] = "√";
                } else {
                    resultData[index]['result'] = "ㄨ";
                }
            }
        }
    }
    user.resultData = resultData;

    console.log("usersfsdbfs", user)
    if (Jurisdiction) {
        res.render("checkpage", {user})
    } else {
        res.render("checkpage", {user})
    }
})
module.exports = router;